package com.huihe.storage.repository.Impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huihe.common.enums.DelFlagEnum;
import com.huihe.storage.entity.SysUser;
import com.huihe.storage.mapper.SysUserMapper;
import com.huihe.storage.repository.SysUserRepository;
import org.springframework.stereotype.Service;

@Service
public class SysUserRepositoryImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserRepository {

    /**
     * 根据用户名查询处用户信息
     * @param account 用户名
     * @return 用户名对应的信息
     */
    @Override
    public SysUser findByUserName(String account) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(account), SysUser::getAccount,account)
                .eq(SysUser::getDelFlag, DelFlagEnum.OK.getCode());
        return getBaseMapper().selectOne(wrapper);
    }

    /**
     * 校验用户名称是否唯一
     * @param account 用户信息
     * @return 结果
     */
    @Override
    public boolean checkUserNameUnique(String account){
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(account), SysUser::getAccount,account)
                .eq(SysUser::getDelFlag, DelFlagEnum.OK.getCode());
        Long count = getBaseMapper().selectCount(wrapper);
        return count == 0;
    }

    /**
     * 注册用户信息
     * @param sysUser 用户信息
     * @return 结果
     */
    @Override
    public boolean registerUser(SysUser sysUser) {
        return getBaseMapper().insert(sysUser) > 0 ;
    }
}
